<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action 	= $_REQUEST['act'];
	$data 		= '';
	$error		= '';

    $client_id	= $_REQUEST['id'];

	switch ($action) {

		case 'get_edit_page':
			$client_id		= $_REQUEST['id'];
			$page 			= GetPage($client_id);
			$data			= array('page'	=> $page);

			break;
		case 'client':
    	    $rResult = mysql_query("SELECT id, name
									FROM client
									WHERE actived=1");

			while ( $aRow = mysql_fetch_assoc( $rResult ) )
			{
				$data['opt'][]= "<option value='$aRow[id]'>$aRow[name]</option>";
			}

		break;
		case 'c_id':
			$data['opt'][]='';
			$start		= $_REQUEST['start'];
			$end		= $_REQUEST['end'];
			$s='';
			$s1='';
			if ($start!='' &  $end!='')
			{

				$s=	"AND DATE(a_letter.end_date) BETWEEN '$start' AND '$end'";
				$s1="AND DATE(d_letter.end_date) BETWEEN '$start' AND '$end'";

			}
			$rResult = mysql_query("SELECT DISTINCT a_letter.invoice_id
									FROM 	a_letter
									JOIN 	client_objects ON a_letter.client_object_id=client_objects.id and client_objects.client_id=$_REQUEST[id]
									WHERE NOT ISNULL(a_letter.invoice_id) $s
									UNION
									SELECT DISTINCT d_letter.invoice_id
									FROM 	d_letter
									JOIN 	client_objects ON d_letter.client_object_id=client_objects.id and client_objects.client_id=$_REQUEST[id]
									WHERE NOT ISNULL(d_letter.invoice_id) $s1");

			$data['opt'][]= "<option value='---'>---</option>";
			while ( $aRow = mysql_fetch_assoc( $rResult ) )
			{
				$data['opt'][]= "<option value='$aRow[invoice_id]'>$aRow[invoice_id]</option>";
			}

		break;
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    $inv="";
    	    $s='';
    	    $s1='';
    	    if ($_REQUEST[inv]!='---' & $_REQUEST[inv]!='0') {
    	    	$inv="and a_letter.invoice_id=$_REQUEST[inv]";
    	    	$inv1="and d_letter.invoice_id=$_REQUEST[inv]";
    	    	
    	    }
    	    $start		= $_REQUEST['start'];
    	    $end		= $_REQUEST['end'];
    	    if ($start!='' &  $end!='')
    	    {

    	    	$s=	"AND DATE(a_letter.end_date) BETWEEN '$start' AND '$end'";
    	    	$s1="AND DATE(d_letter.end_date) BETWEEN '$start' AND '$end'";

    	   }
    	   
$rResult = mysql_query("SELECT 	a_letter.id,
                				client_objects.`name`,
                				production.`name`,
                				IF(b_letter_details.part_type_id = 2,'დამუხტვა',CONCAT(cartridge_parts_type.`name`, 'ს შეცვლა')) AS `part`,
                				COUNT(*),
                				b_letter_details.price,
                				SUM(IF(warehouse_detail.type=2,0,b_letter_details.price))
                FROM  b_letter_details
                JOIN warehouse_detail ON  warehouse_detail.id=b_letter_details.warehouse_detail_id AND warehouse_detail.actived=1
                JOIN a_letter  				ON b_letter_details.a_letter_id=a_letter.id
                JOIN client_objects 	ON a_letter.client_object_id = client_objects.id and client_objects.client_id=$client_id
                JOIN production 			ON warehouse_detail.prod_id = production.id          
                JOIN cartridge_parts_type ON cartridge_parts_type.id = b_letter_details.part_type_id
                WHERE  true $inv $s    
                GROUP BY client_objects.id , production.id, cartridge_parts_type.id	
                UNION ALL	
                SELECT 	d_letter.id,
                				client_objects.`name`,
                				production.`name`,
                				printer_parts_type.`name` AS `part`,
                				COUNT(*),
                				e_letter_details.price,
                				SUM(IF(warehouse_detail.type=2,0,e_letter_details.price))
                FROM  e_letter_details
                JOIN warehouse_detail ON  warehouse_detail.id=e_letter_details.warehouse_detail_id AND warehouse_detail.actived=1
                JOIN d_letter  				ON e_letter_details.d_letter_id=d_letter.id
                JOIN client_objects 	ON d_letter.client_object_id = client_objects.id and client_objects.client_id=$client_id
                JOIN production 			ON warehouse_detail.prod_id = production.id
                JOIN printer_parts_type ON printer_parts_type.id = e_letter_details.part_type_id
                WHERE  true $inv1 $s1    
                GROUP BY client_objects.id , production.id, printer_parts_type.id");

			$data = array(
				"aaData"	=> array()
			);

			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					//General output
					$row[] = $aRow[$i];
				}
				$data['aaData'][] = $row;
			}
	        break;
	    default:
	       	$error =  "null";
	}


	$data['error'] = $error;

	echo json_encode( $data );

	function GetPage($client_id){
		$result	=	 mysql_fetch_array(mysql_query("SELECT	client.`name` as `name`
													FROM	client
													WHERE	client.id = '$client_id'"));
		$data = '
		<div id="dialog-form">
			<fieldset>
				<legend>ძირითადი ინფორმაცია</legend>
				<table width="80%" class="dialog-form-table" cellpadding="10px" >
					<tr align="center">
						<th colspan="2">
							<label for="client_object">კლიენტი  :&nbsp;&nbsp;&nbsp;&nbsp;</label>'.$result['name'].'
						</th>
					</tr>
				</table>
			</fieldset>

			<div class="clear"></div>
			<fieldset>
		    	<legend>ბარათები</legend>
				 <div class="produse">
				    <div class="inner-table">
					    <div id="dt_example" class="ex_highlight_row">
					        <div id="container" class="overhead_container">
					        	<div id="button_area">
					        	</div>
					            <div id="dynamic">
					                <table class="display" id="prod_list" >
					                    <thead>
						                        <tr id="datatable_header">
						                            <th>ID</th>
						                            <th style="width: 170px">ბარათის N</th>
						                            <th style="width: 170px">თარიღი</th>
						                            <th style="width: 100%">თანხა</th>
													<th style="width: 100%">თვითღირებულება</th>
													<th style="width: 100%">სხვაობა</th>
						                        </tr>
					                    </thead>
										<thead>
						                        <tr class="search_header">
							                            <th class="colum_hidden">
							                            	<input type="text" name="search_id" value="ფილტრი" class="search_init" />
							                            </th>
							                            <th>
							                            	<input type="text" name="search_letter" value="ფილტრი" class="search_init" />
							                            </th>
														<th>
															<input type="text" name="search_date" value="ფილტრი" class="search_init" />
														</th>
														<th>
														</th>
							                            <th>
							                            </th>
									 					<th>
							                            </th>
							                     </tr>
					                    </thead>

					                    <tfoot>
					                        <tr>
					                            <th>&nbsp;</th>
					                            <th>&nbsp;</th>
												<th><p align="right">ჯამი:<br/>სულ ჯამი:</th>
					                            <th>&nbsp;</th>
												<th>&nbsp;</th>
					                            <th>&nbsp;</th>
					                        </tr>
					                    </tfoot>
					                </table>
					            </div>
					        </div>
					    </div>
					</div>
				</div>
			</fieldset>
		<!-- ID -->
    </div>
	<div class="inner-table">
		<input type="hidden" style="" name="search_id" class="search_init"  id="client_id" value="'.$client_id.'"/>
	</div';
		return $data;
	}

?>